package com.lu.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lu.article.mapper.ApArticleContentMapper;
import com.lu.article.service.ApArticleFreemarkerService;
import com.lu.article.service.ApArticleIndexService;
import com.lu.common.constants.ApUserExamineContants;
import com.lu.common.constants.ArticleContants;
import com.lu.minio.service.FileStorageService;
import com.lu.model.article.pojo.ApArticle;
import com.lu.model.search.vo.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
@Transactional
public class ApArticleFreemarkerServiceImpl implements ApArticleFreemarkerService {


    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private ApArticleIndexService apArticleIndexService;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;



    /**
     * 生成静态文件上传到minIO中
     * @param apArticle
     * @param content
     */
    @Async
    @Override
    public void buildArticleToMinIO(ApArticle apArticle, String content) {

        if (StringUtils.isNotBlank(content)) {
            //使用freemarker模板生成html文件
            Template template = null;
            StringWriter stringWriter = new StringWriter();
            try {
                template = configuration.getTemplate("article.ftl");
                //数据模型
                Map<String, Object> model = new HashMap<>();
                model.put("text", JSONArray.parseArray(content));
                //合成模板
                template.process(model, stringWriter);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //4.3 把html文件上传到minio中
            InputStream in = new ByteArrayInputStream(stringWriter.toString().getBytes());
            String path = fileStorageService.uploadHtmlFile("", apArticle.getId() + ".html", in);


            //4.4 修改ap_article表，保存static_url字段
            apArticleIndexService.update(Wrappers.<ApArticle>lambdaUpdate().eq(ApArticle::getId,apArticle.getId())
                    .set(ApArticle::getStaticUrl,path));

            //发送消息，创建索引
            createArticleESIndex(apArticle,content,path);
        }

    }


    /**
     * 发送消息，创建索引
     * @param apArticle
     * @param content
     * @param path
     */
    public void createArticleESIndex(ApArticle apArticle, String content, String path) {
        SearchArticleVo vo = new SearchArticleVo();
        BeanUtils.copyProperties(apArticle,vo);
        vo.setContent(content);
        vo.setStaticUrl(path);
        kafkaTemplate.send(ArticleContants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(vo));
    }
}
